package jgespres;

import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

/**
 * Gestionnaire d'Elèves
 * 
 * @author guerfyld
 * @version 2.0
 */
public class JStudentsMgr extends JPanel {
    // Classe à gerer
    private SchoolClass schCls;
    
    // Table d'affichage
    private JTable jStdTable;
    
    /**
     * Constructeur de la classe JStudentMgr
     * 
     * @param schoolClass       Classe à gerer
     */
    public JStudentsMgr(SchoolClass schoolClass) {
        super();
        
        schCls = schoolClass;
        createGUI();
    }
    
    private void createGUI() {
        Font f = new java.awt.Font("Times New Roman", Font.PLAIN, 16);
        // Définition Table des élèves
        JScrollPane tablePane = new JScrollPane();
        jStdTable = new JTable();
        jStdTable.setAutoCreateColumnsFromModel(false);
        jStdTable.setFont(f);
        jStdTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        jStdTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jStdTable.setRowHeight(30);
        jStdTable.setModel(schCls);
        TableColumnModel tcm = new DefaultTableColumnModel(){
            // Fixe largeur des colonnes de la table
            public void addColumn(TableColumn tc) {
                switch (tc.getModelIndex()) {
                    case 0:
                        tc.setPreferredWidth(60);
                        break;
                    case 1:
                        tc.setPreferredWidth(300);
                        break;
                    case 2:
                        tc.setPreferredWidth(100);
                        break;
                    default:
                        tc.setPreferredWidth(50);
                        break;
                }
                super.addColumn(tc);
            }  
        };
        jStdTable.setColumnModel(tcm);
        jStdTable.setTableHeader(new JTableHeader(tcm) {
            // Implemente des Info Bulles sur les Entête de colonnes de la Table
            public String getToolTipText(MouseEvent evt) {
                Point p = evt.getPoint();
                int index = columnModel.getColumnIndexAtX(p.x);
                int realIndex = columnModel.getColumn(index).getModelIndex();
                try {
                    return jgespres.getResourceString("Student.Col" + realIndex + ".tooltip");
                }
                catch(Exception e) {}
                return null;
            }
        });
        jStdTable.createDefaultColumnsFromModel();
        //table.setPreferredScrollableViewportSize(new Dimension(800, 300));
        tablePane.setViewportView(jStdTable);

        // Définition Boutons de gestion de la table des élèves
        JPanel buttonsPane = new JPanel();
        buttonsPane.setLayout( new BoxLayout(buttonsPane, BoxLayout.Y_AXIS) );
        buttonsPane.setPreferredSize(new Dimension(125, 400) );
        // -> Bouton Ajouter
        JButton button = new JButton();
        button.setIcon(new ImageIcon(getClass().getResource("/jgespres/images/add.png")));
        button.setText(jgespres.getResourceString("Student.Add"));
        button.setToolTipText(jgespres.getResourceString("Student.Add.tooltip"));
        button.setHorizontalAlignment(SwingConstants.LEFT);
        button.setIconTextGap(10);
        button.setMaximumSize(new Dimension(125, 30));
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                addStudentAtRow();
            }
        });
        buttonsPane.add(button);
        // -> Bouton Supprimer
        button = new JButton();
        button.setIcon(new ImageIcon(getClass().getResource("/jgespres/images/cancel.png")));
        button.setText(jgespres.getResourceString("Student.Del"));
        button.setToolTipText(jgespres.getResourceString("Student.Del.tooltip"));
        button.setHorizontalAlignment(SwingConstants.LEFT);
        button.setIconTextGap(10);
        button.setMaximumSize(new Dimension(125, 30));
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                delStudentAtRow();
            }
        });
        buttonsPane.add(button);
        // -> Bouton Monter
        button = new JButton();
        button.setIcon(new ImageIcon(getClass().getResource("/jgespres/images/up.png")));
        button.setText(jgespres.getResourceString("Student.Up"));
        button.setToolTipText(jgespres.getResourceString("Student.Up.tooltip"));
        button.setHorizontalAlignment(SwingConstants.LEFT);
        button.setIconTextGap(10);
        button.setMaximumSize(new Dimension(125, 30));
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                upStudentAtRow();
            }
        });
        buttonsPane.add(button);
        // -> Bouton Descendre
        button = new JButton();
        button.setIcon(new ImageIcon(getClass().getResource("/jgespres/images/down.png")));
        button.setText(jgespres.getResourceString("Student.Down"));
        button.setToolTipText(jgespres.getResourceString("Student.Down.tooltip"));
        button.setHorizontalAlignment(SwingConstants.LEFT);
        button.setIconTextGap(10);
        button.setMaximumSize(new Dimension(125, 30));
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                downStudentAtRow();
            }
        });
        buttonsPane.add(button);
        
        setLayout( new BorderLayout(1, 2) );
        add(tablePane, BorderLayout.CENTER);
        add(buttonsPane, BorderLayout.EAST);
    }
    
    private void addStudentAtRow() {
        int row = jStdTable.getSelectedRow();
        int newRow = 0;
        if (row >= 0) {
            schCls.addStudent(new Student());
            newRow = schCls.getNbStudents()-1;
            schCls.moveStudent(newRow, row);
            schCls.fireTableRowsUpdated(row, newRow);
        }
        else if (schCls.getNbStudents() == 0)
            schCls.addStudent(new Student());

        // Force mise à jour affichage JTable
        schCls.fireTableRowsInserted(newRow, newRow);
    }
    
    private void delStudentAtRow() {
        int row = jStdTable.getSelectedRow();
        if (row >= 0) {
            schCls.delStudent(row);

            // Force mise à jour affichage JTable
            schCls.fireTableRowsDeleted(row, row);
        }    
    }
    
    private void upStudentAtRow() {
        int row = jStdTable.getSelectedRow();
        if (row > 0) {
            schCls.moveStudent(row, row-1);

            // Force mise à jour affichage JTable
            schCls.fireTableRowsUpdated(row-1, row);
            jStdTable.setRowSelectionInterval(row-1, row-1);
        }
    }
    
    private void downStudentAtRow() {
        int row = jStdTable.getSelectedRow();
        if (row < jStdTable.getRowCount()-1 && row >= 0) {
            schCls.moveStudent(row, row+2);

            // Force mise à jour affichage JTable
            schCls.fireTableRowsUpdated(row, row+1);
            jStdTable.setRowSelectionInterval(row+1, row+1);
        }
    }
}
